"""
Copyright 2012 Wieser Dietmar, Margit Mutschlechner.

This file is part of Multimedia Social Networking Platform.

Multimedia Social Networking Platform is free software: 
you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Multimedia Social Networking Platform is distributed
in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Multimedia Social Networking Platform.  
If not, see <http://www.gnu.org/licenses/>.

"""

import unittest
from msnp.persistence import PersistenceController
from msnp.model import MediaModel

class TestMediaAccessor(unittest.TestCase):
    def test(self):
        PersistenceController.initMediaDatabase()
        #INSERTgroupname
        audio = self.__testInsertAudio()
        #SELECT
        self.__testSelectArtistForName(audio)
        self.__testSelectCollectionForNameAndArtist(audio)
        self.__testSelectGenreForName(audio)
        self.__testSelectAudiosForTitleArtistAndCollection(audio)
        self.__testSelectRatingForMediaIdAndUsername(audio)
        #UPDATE
        self.__testUpdateArtist(audio)
        self.__testUpdateGenre(audio)
        self.__testUpdateCollection(audio)
        self.__testUpdateRating(audio)
        self.__testUpdateMedia(audio)
        self.__testUpdateAudio(audio)
        
    ############
    ## SELECT ##
    ############
        
    def __testSelectArtistForName(self, audio):
        artist = PersistenceController.selectArtistForName(audio.getArtist().getName())
        self.assertEqual(audio.getArtist().getName(), artist.getName())
        self.assertEqual(audio.getArtist().getId(), artist.getId())
        #test return value if not in db
        artist = PersistenceController.selectArtistForName("not in db")
        self.assertEqual(artist, None)
        
    def __testSelectCollectionForNameAndArtist(self, audio):
        collection = PersistenceController.selectCollectionForNameAndArtist(audio.getMedia().getCollection().getName(), audio.getArtist().getName())
        self.assertEqual(collection.getName(), audio.getMedia().getCollection().getName())
        self.assertEqual(collection.getId(), audio.getMedia().getCollection().getId())
        #test return value if artist not in db
        collection = PersistenceController.selectCollectionForNameAndArtist(audio.getMedia().getCollection().getName(), "not in db")
        self.assertEqual(collection, None)
        #test return value if collection not in db
        collection = PersistenceController.selectCollectionForNameAndArtist("not in db", audio.getArtist().getName())
        self.assertEqual(collection, None)
        
    def __testSelectGenreForName(self, audio):
        genre = PersistenceController.selectGenreForName(audio.getMedia().getGenre().getName())
        self.assertEqual(genre.getName(), audio.getMedia().getGenre().getName())
        self.assertEqual(genre.getId(), audio.getMedia().getGenre().getId())
        #test return value if not in db
        genre = PersistenceController.selectGenreForName("not in db")
        self.assertEqual(genre, None)
        
    def __testSelectAudiosForTitleArtistAndCollection(self, audio):
        title = audio.getMedia().getTitle()
        artistName = audio.getArtist().getName()
        collectionName = audio.getMedia().getCollection().getName()
        audios = PersistenceController.selectAudiosForTitleArtistAndCollection(title, artistName, collectionName)
        self.assertEqual(1, len(audios))
        a = audios[0]
        self.assertEqual(audio.getId(), a.getId())
        #title not in db
        audios = PersistenceController.selectAudiosForTitleArtistAndCollection("not in db", artistName, collectionName)
        self.assertEqual(audios, None)
        #artist not in db
        audios = PersistenceController.selectAudiosForTitleArtistAndCollection(title, "not in db", collectionName)
        self.assertEqual(audios, None)
        
    def __testSelectRatingForMediaIdAndUsername(self, audio):
        mediaId = audio.getMedia().getId()
        username = audio.getMedia().getRatings()[0].getUsername()
        rating = PersistenceController.selectRatingForMediaIdAndUsername(mediaId, username)
        self.assertEqual(rating.getUsername(), username)
        self.assertEqual(rating.getMediaId(), mediaId)
        #username not in db
        rating = PersistenceController.selectRatingForMediaIdAndUsername(mediaId, "not in db")
        self.assertEqual(rating, None)
        
    ############
    ## UPDATE ##
    ############
    def __testUpdateArtist(self, audio):
        oldArtist = audio.getArtist()
        newArtist = MediaModel.Artist.createArtist(oldArtist.getName() + "new", oldArtist.getId())
        updated = PersistenceController.updateArtist(oldArtist, newArtist)
        self.assertEqual(updated, MediaModel.ReturnType.UPDATED)
        
    def __testUpdateCollection(self, audio):
        oldCol = audio.getMedia().getCollection()
        newCol = MediaModel.Collection.createCollection(oldCol.getName() + "new", oldCol.getId())
        updated = PersistenceController.updateCollection(oldCol, newCol)
        self.assertEqual(updated, MediaModel.ReturnType.UPDATED)
        
    def __testUpdateGenre(self, audio):
        oldGenre = audio.getMedia().getGenre()
        newGenre = MediaModel.Genre.createGenre(oldGenre.getName() + "new", oldGenre.getId())
        updated = PersistenceController.updateGenre(oldGenre, newGenre)
        self.assertEqual(updated, MediaModel.ReturnType.UPDATED)
        
    def __testUpdateRating(self, audio):
        oldRating = audio.getMedia().getRatings()[0]
        newRating = MediaModel.Rating.createRating(6, 6, 6, oldRating.getId(), oldRating.getUsername(), oldRating.getMediaId())
        updated = PersistenceController.updateRating(oldRating, newRating)
        self.assertEqual(updated, MediaModel.ReturnType.UPDATED)
        
    def __testUpdateMedia(self, audio):
        oldMedia = audio.getMedia()
        newMedia = MediaModel.Media.createMedia(oldMedia.getTitle() + "new", "2012", oldMedia.getComposer() + "new", oldMedia.getCollection(), oldMedia.getGenre(), 3, 9, oldMedia.getRatings(), oldMedia.getMediaType(), oldMedia.getId())
        updated = PersistenceController.updateMedia(oldMedia, newMedia)
        self.assertEqual(updated, MediaModel.ReturnType.UPDATED)
        
    def __testUpdateAudio(self, audio):
        oldAudio = audio
        oldMedia = audio.getMedia()
        oldArtist = audio.getArtist()
        oldCol = audio.getMedia().getCollection()
        oldGenre = audio.getMedia().getGenre()
        newAudio = MediaModel.Audio.createAudioFromMediaDb(oldAudio.getId(), oldAudio.getLyrics() + "new", 4, 3, 6, 130, oldMedia.getId(), oldMedia.getMediaType(), oldMedia.getTitle(), oldMedia.getYear(), oldMedia.getComposer(), oldMedia.getLength(), None, oldMedia.getFilesize(), oldArtist.getId(), oldArtist.getName(), oldGenre.getId(), oldGenre.getName(), oldCol.getId(), oldCol.getName(), oldMedia.getRatings())
        updated = PersistenceController.updateAudio(oldAudio, newAudio)
        self.assertEqual(updated, MediaModel.ReturnType.UPDATED)
        
    ############
    ## INSERT ##
    ############
    
    def __testInsertAudio(self):
        audio = self.__createAudio()
        artistId = PersistenceController.insertArtist(audio.getArtist())
        genreId = PersistenceController.insertGenre(audio.getMedia().getGenre())
        collectionId = PersistenceController.insertCollection(audio.getMedia().getCollection())
        audio.getArtist().setId(artistId)
        audio.getMedia().getGenre().setId(genreId)
        audio.getMedia().getCollection().setId(collectionId)
        mediaId = PersistenceController.insertMedia(audio.getMedia())
        audio.getMedia().setId(mediaId)
        audioId = PersistenceController.insertAudio(audio)
        audio.setId(audioId)
        ratings = self.__createRatings()
        audio.getMedia().setRatings(ratings)
        for rating in ratings:
            ratingId = PersistenceController.insertRating(rating, mediaId)
            rating.setId(ratingId)
        audio.getMedia().setRatings(ratings)
        return audio
        
    
    def __createAudio(self):
        artist = self.__createArtist()
        media = self.__createMedia()
        a = MediaModel.Audio(None, media, artist, "comment", "lyrics", 2, 3, 4, 120)
        return a
        
    def __createMedia(self):
        genre = self.__createGenre()
        collection = self.__createCollection()
        m = MediaModel.Media(None, MediaModel.MediaType.AUDIO, "testmedia", "2011", "testcomposer", collection, genre, 10, 1000, None)
        return m
    
    def __createArtist(self):
        a = MediaModel.Artist(None, "testartist")
        return a 
    
    def __createGenre(self):
        g = MediaModel.Genre(None, "testgenre")
        return g
    
    def __createCollection(self):
        c = MediaModel.Collection(None, "testcollection")
        return c
    
    def __createRatings(self):
        r1 = MediaModel.Rating(None, None, "user1", 2, 3, 4)
        r2 = MediaModel.Rating(None, None, "user2", 5, 6, 7)
        ratings = []
        ratings.append(r1)
        ratings.append(r2)
        return ratings
    
def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestMediaAccessor))
    return suite

if __name__ == '__main__':
    suiteFew = unittest.TestSuite()
    unittest.TextTestRunner(verbosity=2).run(suite())
